import {defineStore} from 'pinia';

const sysBuiltInOption = [
    {label: "是", value: "1"},
    {label: "否", value: "0"},
]

const API = {
    LIST: "/param/page",
    ENABLED: "/param/enabled",
    DISABLED: "/param/disabled",
    ADD: "/param/save",
    INFO: "/param/get",
    MODIFY: "/param/modify",
    REMOVE: "/param/remove",
}

const COLUMNS = [
    {field: "name", title: "参数名称", align: "center", width: "150px"},
    {field: "id", title: "参数键名", align: "center", width: "150px"},
    {field: "value", title: "参数键值", align: "center", width: "140px"},
    {field: "typeName", title: "系统内置", align: "center", width: "140px"},
    {field: "remark", title: "备注", align: "center", width: "150px"},
    {field: "status", title: "状态", align: "center", width: "150px"},
    {field: "createTime", title: "创建时间", align: "center"},
]

const SEARCH_FORM = [
    {field: "name", title: "参数名称", placeholder: "请输入参数名称"},
    {field: "id", title: "参数键名", placeholder: "请输入参数键名"},
    {field: "type", title: "系统内置", options: sysBuiltInOption},
    {mapper: [{field: "beginTime", placeholder: "请选择开始日期"}, {field: "endTime", placeholder: "请选择结束日期"}], title: "创建时间", type: 'dateRange'},
]

const OPTIONS = [
    {emit: "modify", title: "修改", check: (row) => { return row.type == 0 }},
    {emit: "remove", title: "删除", check: (row) => { return row.type == 0 }},
]

const store = {
    id: "parameter",
    state(){
        return {
            table: null,
            pageNo: 1,
            pageSize: 10,
            searchForm: {}
        }
    },

    getters: {

    },

    actions: {
        init(table) {
            this.table = table;
            this.table.setColumns(COLUMNS);
            this.table.setSearchForm(SEARCH_FORM);
            this.table.setOptions(OPTIONS);
        },

        refresh(isFirstPage) {
            if (isFirstPage) {
                this.getList(1, 10)
                return
            }
            this.getList(this.pageNo, this.pageSize)
        },

        /**
         * 列表数据
         * @param pageNo
         * @param pageSize
         * @param condition
         * @returns {Promise<void>}
         */
        async getList(pageNo, pageSize, condition) {
            let params = {};
            params.pageNo = pageNo ? pageNo : 1;
            params.pageSize = pageSize ? pageSize : 10;
            for (let key in condition) {
                if(! Helper.empty(condition[key])) {
                    params[key] = condition[key];
                }
            }

            Msg.loading("加载中...")
            let {data} = await Http.get(API.LIST, params);
            Msg.loading(false)

            this.pageNo = data.pageNo
            this.pageSize = data.pageSize
            let dataList = []
            if(! Helper.emptyArray(data.dataList)) {
                dataList = data.dataList
            }
            this.table.setData(dataList);
            this.table.setPagination(data.pageNo, data.pageSize, data.total);
        },

        /**
         * 状态启用
         * @param params
         * @returns {Promise<boolean>}
         */
        async enabled(params) {
            Msg.loading("加载中...")
            let {code} = await Http.post(API.ENABLED, params);
            Msg.loading(false);

            if(code == 200) {
                Msg.success("保存成功");
                this.refresh();
                return true
            } else {
                Msg.error("保存失败，请重新尝试")
                return false
            }
        },

        /**
         * 状态禁用
         * @param params
         * @returns {Promise<boolean>}
         */
        async disabled(params) {
            Msg.loading("加载中...")
            let {code} = await Http.post(API.DISABLED, params);
            Msg.loading(false);

            if(code == 200) {
                Msg.success("保存成功");
                this.refresh();
                return true
            } else {
                Msg.error("保存失败，请重新尝试")
                return false
            }
        },

        /**
         * 新增
         * @param params
         * @returns {Promise<undefined>}
         */
        async add(params) {
            Msg.loading("正在提交...")
            let {data} = await Http.post(API.ADD, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.refresh(true);
            } else {
                Msg.error("保存失败, 请重新尝试")
            }
            return data
        },

        /**
         * 修改
         * @param params
         * @returns {Promise<undefined>}
         */
        async modify(params) {
            Msg.loading("正在提交...")
            let {data} = await Http.post(API.MODIFY, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.refresh(true);
            } else {
                Msg.error("保存失败, 请重新尝试")
            }
            return data
        },

        /**
         * 获取详情
         * @param id
         * @returns {Promise<undefined|boolean>}
         */
        async getInfo(id) {
            Msg.loading("加载中...");
            let {data} = await Http.get(API.INFO, {id: id})
            Msg.loading(false);

            if (! Helper.emptyJson(data)) {
                return data
            }
            return false
        },

        /**
         * 删除
         * @param id
         * @returns {Promise<undefined>}
         */
        async remove(id) {
            Msg.loading("正在删除...");
            let {data} = await Http.post(API.REMOVE, {id: id})
            Msg.loading(false)

            if(data) {
                Msg.success("删除成功");
                this.refresh()
            } else {
                Msg.error("删除失败，请重新尝试");
            }
            return data
        },
    },
}

export default defineStore(store)